package archimede;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;

public class PuntiRilevazione extends JTabbedPane {

  private JButton plus = new JButton(new ImageIcon(ClassLoader.getSystemResource("plus.png")));
  private int count = 0;
  private StartingWindow st;

  protected void paintComponent(Graphics g) {
    setOpaque(false);
    g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
    super.paintComponent(g);
  }

  public PuntiRilevazione(final StartingWindow st) {
    super();
    this.st = st;
    plus.setBorder(null);
    plus.setContentAreaFilled(false);
    plus.setRolloverEnabled(true);
    plus.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("plus_roll.png")));
    plus.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        addTab("M" + (++count) + "   ", new JScrollPane(new ObjM(st, count),
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
          @Override
          public void setVerticalScrollBar(JScrollBar verticalScrollBar) {
            // TODO Auto-generated method stub
            verticalScrollBar.setUnitIncrement(15);
            super.setVerticalScrollBar(verticalScrollBar);
          }
        });
        setTabComponentAt(count, new ButtonTabObjM(PuntiRilevazione.this));
      }
    });
    addTab("", null);
    setTabComponentAt(count, plus);

  }

  public String print() {
    String print = "\n#" + count;
    for (int i = 1; i <= count; i++) {
      print = print
          + ((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView())
              .print();
    }
    print = print + "\n@@Punti rilevazione";
    return print;
  }

  public boolean save() {
    boolean done = true;
    for (int i = 1; i <= count; i++) {
      if (!((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView())
          .save())
        done = false;
    }
    return done;
  }

  public void setElement(String data) {
    Scanner c = new Scanner(data);
    c.nextLine();
    count = Integer.parseInt(c.nextLine().substring(1));
    for (int i = 1; i <= count; i++) {
      ObjM tmp = new ObjM(st, i);
      String value = c.nextLine().substring(1);
      boolean next = true;
      while (c.hasNextLine() && next) {
        String line = c.nextLine();
        if (line.equalsIgnoreCase("#endm")) {
          tmp.setElement(value);
          next = false;
        }
        value = value + "\n" + line;
      }
      // System.out.println("MMMMMMM : "+i+"\n"+value);
      addTab("M" + (i), new JScrollPane(tmp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
          JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
        @Override
        public void setVerticalScrollBar(JScrollBar verticalScrollBar) {
          // TODO Auto-generated method stub
          verticalScrollBar.setUnitIncrement(15);
          super.setVerticalScrollBar(verticalScrollBar);
        }
      });
      setTabComponentAt(i, new ButtonTabObjM(PuntiRilevazione.this));
    }
    setSelectedIndex(count);

  }

  public Document pdf(Document doc) {
    doc.newPage();
    Chunk underline = new Chunk(
        "6. Report termografico e rilievo umidità con metodo ponderale Darr", FontFactory.getFont(
            FontFactory.TIMES, 16, Font.BOLD));
    underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

    Anchor anchorTarget = new Anchor(underline);
    anchorTarget.setName("m");
    Paragraph targetParagraph = new Paragraph();
    targetParagraph.add(anchorTarget);
    try {
      doc.add(targetParagraph);
    } catch (DocumentException e2) {
      e2.printStackTrace();
    }
    for (int i = 1; i <= count; i++) {
      ((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView())
          .pdf(doc, i);
      doc.newPage();
    }

    return doc;
  }

  public void replace(String newName, String oldName) {
    for (int i = 1; i <= count; i++) {
      ((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView()).replace(
          newName, oldName);
    }
  }

  public void setCount(int c, int rem) {
    for (int i = rem + 1; i <= count; i++) {
      ((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView())
          .changeM(i - 1);
    }
    count = c;
  }

  public int getCount() {
    return count;
  }
}
